package com.tanhua.app.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.FriendApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.autoconfig.template.HuanXinTemplate;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageBeanVo;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Autowired
    private CommentService commentService;

    public ResponseEntity addContacts(Long friendId) {
        //向mongo 中保存好友
        friendApi.save(UserHolder.getUserId(),friendId);
        // 向环信中保存好友
        huanXinTemplate.addContacts(UserHolder.getUserId(),friendId);
        return  ResponseEntity.ok(null);

    }

    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        // 1、调用api查询好友分页
        PageBeanVo pageBeanVo = friendApi.findByPage(pageNum, pageSize, UserHolder.getUserId());

        // 2、封装userInfo = vo
        List<ContactVo> voList = new ArrayList<>();
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                ContactVo vo = new ContactVo();
                UserInfo friendInfo = userInfoApi.fingByUserId(friend.getFriendId());
                BeanUtil.copyProperties(friendInfo,vo ); // 设置好友基本信息
                vo.setUserId(friend.getFriendId().toString()); // 设置好友id
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
    @GetMapping(value = "/messages/likes",name = "被点赞用户列表")
    public ResponseEntity messagesLikes(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        Integer commentType = 1;
        return commentService.findUserCommentVoByPage(pageNum,pageSize,commentType);
    }

    @GetMapping(value = "/messages/comments",name = "被评论户用户列表")
    public ResponseEntity messagesComments(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        Integer commentType = 2;
        return commentService.findUserCommentVoByPage(pageNum,pageSize,commentType);
    }
    @GetMapping(value = "/messages/loves",name = "被点喜欢用户列表")
    public ResponseEntity messagesLoves(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        Integer commentType = 3;
        return commentService.findUserCommentVoByPage(pageNum,pageSize,commentType);
    }
}
